import pygame
import sys

# 初始化pygame
pygame.init()

# 设置窗口大小
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("战士移动、跳跃和攻击")

# 定义颜色
WHITE = (255, 255, 255)
GRAVITY = 0.5  # 重力加速度

# 加载战士的图片
warrior_image_right = pygame.image.load("pictures/student_right.png")  # 向右走的图片
warrior_image_left = pygame.image.load("pictures/student_left.png")    # 向左走的图片
warrior_image_jump = pygame.image.load("pictures\strike1.png")    # 跳跃的图片
warrior_image_attack = pygame.image.load("pictures\warrior.jpg")  # 攻击的图片

# 调整图片大小
warrior_image_right = pygame.transform.scale(warrior_image_right, (150, 150))
warrior_image_left = pygame.transform.scale(warrior_image_left, (150, 150))
warrior_image_jump = pygame.transform.scale(warrior_image_jump, (150, 150))
warrior_image_attack = pygame.transform.scale(warrior_image_attack, (150, 150))

# 定义战士的初始位置和速度
warrior_width, warrior_height = warrior_image_right.get_size()
warrior_x = width // 2
warrior_y = height - warrior_height
warrior_speed_x = 0
warrior_speed_y = 0
is_jumping = False
facing_right = True  # 默认朝向右边
is_attacking = False  # 是否正在攻击
attack_duration = 10  # 攻击动作的持续时间
attack_timer = 0  # 攻击计时器

# 主循环
clock = pygame.time.Clock()
while True:
    screen.fill(WHITE)  # 清屏

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        # 键盘按下事件
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                warrior_speed_x = -5  # 向左移动
                facing_right = False
            if event.key == pygame.K_RIGHT:
                warrior_speed_x = 5  # 向右移动
                facing_right = True
            if event.key == pygame.K_UP and not is_jumping:
                warrior_speed_y = -15  # 跳跃初速度
                is_jumping = True
            if event.key == pygame.K_SPACE and not is_attacking:
                is_attacking = True  # 开始攻击
                attack_timer = attack_duration  # 重置攻击计时器

        # 键盘松开事件
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT and warrior_speed_x < 0:
                warrior_speed_x = 0  # 停止左移
            if event.key == pygame.K_RIGHT and warrior_speed_x > 0:
                warrior_speed_x = 0  # 停止右移

    # 更新战士的位置
    warrior_x += warrior_speed_x
    warrior_y += warrior_speed_y

    # 重力效果
    if is_jumping:
        warrior_speed_y += GRAVITY
        if warrior_y >= height - warrior_height:  # 落地
            warrior_y = height - warrior_height
            warrior_speed_y = 0
            is_jumping = False

    # 边界检测
    if warrior_x < 0:
        warrior_x = 0
    if warrior_x > width - warrior_width:
        warrior_x = width - warrior_width

    # 更新攻击状态
    if is_attacking:
        attack_timer -= 1
        if attack_timer <= 0:
            is_attacking = False  # 攻击结束

    # 根据状态选择战士的图片
    if is_attacking:
        current_warrior_image = warrior_image_attack
    elif is_jumping:
        current_warrior_image = warrior_image_jump
    elif facing_right:
        current_warrior_image = warrior_image_right
    else:
        current_warrior_image = warrior_image_left

    # 绘制战士
    screen.blit(current_warrior_image, (warrior_x, warrior_y))

    # 刷新屏幕
    pygame.display.flip()
    clock.tick(60)  # 60帧每秒